<?php
/**
 * @name IndexController
 * @author 6p2b7gaqkyoe4aw\administrator
 * @desc 默认控制器
 * @see http://www.php.net/manual/en/class.yaf-controller-abstract.php
 */
error_reporting(E_ALL^E_NOTICE^E_WARNING);
class OrderController extends Yaf_Controller_Abstract {

	public function getOrderListAction(){
		$request=json_decode(file_get_contents("php://input"),true);
		$page = $request['page'] ? $request['page'] : 1;
		$pagesize = $request['pagesize'] ? $request['pagesize'] : 10;
		
		//0未付款 1已付 2发货 3确认
		$statusArr = ['0' => '未付款', '1' => '已付', '2' => '发货', '3' => '确认'];
		$where = [];
		
		$model = new OrderModel();
		$list = $model->getList($where, $page, $pagesize);
		
		$orderids = [];
		foreach($list as &$val){
			$orderids[] = $val['oid'];
			$val['status'] = $statusArr[$val['status']];
			$val['ctime'] = date('Y-m-d H:i:s', $val['ctime']);
		}
		
		$orderGoodsModel = new OrderGoodsModel();
		$orderGoodsList = $orderGoodsModel->getListByIds($orderids);
		
		foreach($list as &$val){
			$val['goods'] = $orderGoodsList[$val['oid']];
		}
		
		$count = $model->getCount($where);
		
		echo json_encode(['total' => $count, 'list' => $list]);
	}
	
	
	public function getOrderAction(){
		$request = json_decode(file_get_contents("php://input"),true);
		$statusArr = ['0' => '未付款', '1' => '已付', '2' => '发货', '3' => '确认'];
		$oid = $request['oid'];
		
		$model = new OrderModel();
		$where = [];
		if($oid){
			$where['oid = ?'] = $oid;
		}
		
		$rs = $model->getOne($where);
		
		$orderGoodsModel = new OrderGoodsModel();
		$orderGoodsList = $orderGoodsModel->getListByIds([$rs['oid']]);
		$rs['goods'] = $orderGoodsList[$rs['oid']];
		$rs['status'] = $statusArr[$rs['status']];
		$rs['ctime'] = date('Y-m-d H:i:s', $rs['ctime']);
		
		echo json_encode($rs);
	}
	
	
}
